/** @noSelfInFile **/
import {PlayerModel} from "./apiModel/adapterModel/model4/PlayerModel";
import {UnitModel} from "./apiModel/adapterModel/model3/UnitModel";
import {PlayerJson} from "./apiModel/model/StoreJson";
import Configer from "./apiModel/config/Configer";
import {ConfigKey} from "../constant/ConfigConstant";
import {GlobalModel} from "./apiModel/adapterModel/model1/GlobalModel";
import {playerAdapter} from "./apiModel/adapter/adapterInterface/model4/IPlayerAdapter";

export class PlayerFactory {
    players: PlayerModel[] = [];
    static playerFactory: PlayerFactory = new PlayerFactory();

    get maxPlayerId() {
        return 15
    }

    static getInstance() {
        return PlayerFactory.playerFactory;
    }

    initPlayers() {
        const initCoin = Configer.getConfig(ConfigKey.initCoin);
        const initWood = Configer.getConfig(ConfigKey.initWood);
        const initHeight = Configer.getConfig(ConfigKey.initHeight);
        const initAngle = Configer.getConfig(ConfigKey.initAngle);
        const playerJson: PlayerJson = { c: initCoin, h: initHeight, w: initWood, a: initAngle }
        for (let playerId = 0; playerId < this.maxPlayerId + 1; playerId ++) {
            const playerModel = new PlayerModel(playerId, playerJson);
            this.players[playerId] = playerModel;
        }
    }

    getUnitOwner(unit: UnitModel) {
        if (unit?.isValid()) {
            const playerId = unit.getOwnerId();
            const playerModel = this.getPlayer(playerId);
            return playerModel;
        }
    }

    getPlayer(playerId: number): PlayerModel {
        return this.players[playerId];
    }

    getFirstPlayer(): PlayerModel {
        return this.players[0];
    }

    getLocalPlayer() {
        const playerId = playerAdapter.adapter.getLocalPlayerId();
        return this.getPlayer(playerId);
    }

    getPlayingPlayerNum() {
        let count = 0;
        this.mapAllNotEndPlayingPlayer(
            (player) => {
                if (player?.isValid()) {
                    count = count + 1;
                }
            }
        )
        return count;
    }

    getAllPlayerNum() {
        let count = 0;
        this.mapAllPlayer(
            (player) => {
                if (player?.isValid()) {
                    count = count + 1;
                }
            }
        )
        return count;
    }

    getAllNotEndPlayingPlayer() {
        const players: PlayerModel[] = [];
        const fun = (player: PlayerModel) => {
            players.push(player);
        }
        this.mapAllNotEndPlayingPlayer(fun);
        return players;
    }

    mapAllNotEndPlayingPlayer(fun: (player: PlayerModel) => void) {
        for (let i = 0; i < this.maxPlayerId; i++) {
            let tempPlayer = PlayerFactory.getInstance().getPlayer(i);
            if (tempPlayer) {
                if (tempPlayer.isNotEndPlaying()) {
                    fun(tempPlayer);
                }
            }
        }
    }
    mapAllPlayer(fun: (player: PlayerModel) => void) {
        for (let i = 0; i < this.maxPlayerId; i++) {
            let tempPlayer = PlayerFactory.getInstance().getPlayer(i);
            if (tempPlayer) {
                fun(tempPlayer);
            }
        }
    }

    getUserCount(): number {
        let count = 0
        for (let i = 0; i < this.maxPlayerId; i++) {
            let tempPlayer = PlayerFactory.getInstance().getPlayer(i);
            if (tempPlayer.isPlaying()) {
                count++;
            }
        }
        return count;
    }

    startCheckSync() {
        playerAdapter.adapter.startCheckSync()
    }
}
